package com.prism.moodme.util;

import java.util.List;
import java.util.Calendar;
import java.util.GregorianCalendar;

import com.prism.moodme.data.Data;


public class DisplayUtility {
  public static String getMapURL(List<Data> dataSet, int height, int width) {
    String urlPrefix = "http://maps.google.com/maps/api/staticmap?zoom=14&maptype=roadmap&sensor=true&chtt=GPS+Distribution";
    String location = "";
    double meanLat = 0;
    double meanLong = 0;
    int count = 0;

    for (Data data: dataSet) {
    	if(count>40)
    		break;
      double latitude = data.getGpsLat();
      double longitude = data.getGpsLong();
      location = location + "&markers=" + Double.toString(latitude) + "," + Double.toString(longitude);
      meanLat = meanLat + latitude;
      meanLong = meanLong + longitude;
      count = count + 1;
    }
    String dims =  "&size=" + Integer.toString(width)+"x" + Integer.toString(height) ;
    String center = "&center=" + Double.toString((meanLat/count)) + "," + Double.toString((meanLong/count));
    String url = urlPrefix + center + dims + location;
	return url;
  }

  public static String getDayChartURL(List<Data> dataSet, int height, int width) {
	String urlPrefix = "https://chart.googleapis.com/chart?&chxt=x,y,r&cht=bvs&chbh=a,5&chf=c,s,000000|bg,s,EFEFEF&chtt=Daily+Frequency+Distribution";
	String color = "&chf=c,s,000000|bg,s,EFEFEF|b0,lg,0,FF0000,0,0000FF,1";
	String days = "&chxl=0:|Sun|Mon|Tue|Wed|Thu|Fri|Sat";
	int [] day_count = new int[7];
	for (Data data: dataSet) {
      Calendar calendar = Utility.getTime(data.getTime());
	  int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
      day_count[dayOfWeek] = day_count[dayOfWeek] + 1;
	}
	String dayFreqs = "&chd=t:";
	for (int i = 0; i < 7; i++) {
      dayFreqs = dayFreqs + Integer.toString(day_count[i]);
      if (i!=6) {
    	dayFreqs = dayFreqs + ",";
      }
	}
	String dims =  "&chs=" + Integer.toString(width)+"x" + Integer.toString(height);
    String url = urlPrefix + days + dims + dayFreqs + color;
    return url;
  }

  public static String getTimeChartURL(List<Data> dataSet, int height, int width) {
	String urlPrefix = "https://chart.googleapis.com/chart?chxt=x,y&cht=bvs&chbh=a,5&chtt=Hourly+Frequency+Distribution";
	String color = "&chf=c,s,000000|bg,s,EFEFEF|b0,lg,0,FF0000,0,0000FF,1";
	String hours = "&chxl=0:|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24";
	int [] hour_count = new int[24];
	for (Data data: dataSet) {
		Calendar calendar = Utility.getTime(data.getTime());
		hour_count[calendar.get(Calendar.HOUR_OF_DAY)] = hour_count[calendar.get(Calendar.HOUR_OF_DAY)] + 1;
	}
	String hourFreqs = "&chd=t:";
	for (int i = 0; i < 24; i++) {
	  hourFreqs = hourFreqs + Integer.toString(hour_count[i]);
	  if (i!=23) {
	   	hourFreqs = hourFreqs + ",";
	  }
	}
	String dims =  "&chs=" + Integer.toString(width)+"x" + Integer.toString(height) ;
	String url = urlPrefix + hours + dims + hourFreqs + color;
	return url;
  }
}
